import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { IProjectToEmployeeLink } from '../model/interfaces'
import { projectEmployeeLinksApi } from '../api/projectEmployeeLinksApi'

interface IProjectToEmployeeLinksState {
  projectEmployeeLinks: IProjectToEmployeeLink[]
}

const initialState: IProjectToEmployeeLinksState = {
  projectEmployeeLinks: []
}

export const projectEmployeeLinksSlice = createSlice({
  name: 'projectEmployeeLinks',
  initialState,
  reducers: {
    setProjectToEmployeeLinks(state, {payload}: PayloadAction<IProjectToEmployeeLink[]>) {
      state.projectEmployeeLinks = payload
    }
  },
  extraReducers: (builder) => {
    builder.addMatcher(projectEmployeeLinksApi.endpoints?.getProjectEmployeeLinks.matchFulfilled, (state, {payload}: PayloadAction<IProjectToEmployeeLink[]>) => {
      state.projectEmployeeLinks = payload
    })
  }
})

export const projectEmployeeLinksActions = projectEmployeeLinksSlice.actions
export const projectEmployeeLinksReducer = projectEmployeeLinksSlice.reducer
